package com.example.multiple.config;

import com.example.multiple.util.HashUtil;
import io.shardingsphere.api.algorithm.sharding.PreciseShardingValue;
import io.shardingsphere.api.algorithm.sharding.standard.PreciseShardingAlgorithm;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;

/**
 * 数据库映射计算
 */
@Slf4j
public class DataSourceAlg implements PreciseShardingAlgorithm<String> {

    @Override
    public String doSharding(Collection<String> names, PreciseShardingValue<String> value) {
        log.info("分库算法参数 {},{}",names,value);
        int hash = HashUtil.rsHash(String.valueOf(value.getValue()));
        String index = "ds_" + ((hash % 2));
        log.info("对应的分库：{}",index);
        return index;
    }
}
